package com.utils;

import java.io.InputStream;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.mybatisSimple.demo.UnitTest;

public class MyBatisUtil {

	
	/*
	 * 获取 session factory
	 * */
	public static SqlSessionFactory getSqlSessionFactory(){
		
        //mybatis的配置文件
        String resource = "config/conf.xml";
        
        //使用类加载器加载mybatis的配置文件
        InputStream is = UnitTest.class.getClassLoader().getResourceAsStream(resource);
        
        //构建sqlSession的工厂
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        
        return sessionFactory;
	}
	
	
	/*
	 * 获取 SqlSession
	 * */
	public static SqlSession getSqlSession(){
		return MyBatisUtil.getSqlSessionFactory().openSession();
	}
	
	
	/*
	 * 获取 SqlSession, 带 autocommit 标志位
	 * 
	 * */
	public static SqlSession getSqlSession(boolean autocommit){
		return MyBatisUtil.getSqlSessionFactory().openSession(autocommit);
	}
}
